package unimb.feri;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;

public class MainFrame extends TabActivity {
	protected CTools tools;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_refresh, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.osvezi:
			Refresh();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void Refresh() {
		startActivity(new Intent(this, MainFrame.class));
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		tools = new CTools(this);

		setContentView(R.layout.main);
		Resources res = getResources();

		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent().setClass(this, Map.class);
		spec = tabHost.newTabSpec("map");
		spec.setIndicator("Zemljevid", res.getDrawable(R.drawable.map));
		Context ctx = this.getApplicationContext();
		Intent i = new Intent(ctx, Map.class);
		spec.setContent(i);
		tabHost.addTab(spec);

		CPath p = tools.getPath();
		if (p != null && !p.idUser.equals("") && p.fromLat != 0.0) {
			int E6 = 1000000;
			intent = new Intent(android.content.Intent.ACTION_VIEW,
					Uri.parse("http://maps.google.com/maps?saddr=" + p.fromLat
							+ "," + p.fromLon + "&daddr=" + p.toLat / E6 + ","
							+ p.toLon / E6));

			spec = tabHost
					.newTabSpec("navigation")
					.setIndicator("Navigacija",
							res.getDrawable(R.drawable.compass_icon))
					.setContent(intent);
			tabHost.addTab(spec);
		}

		// intent = new Intent().setClass(this, Navigation.class);

		intent = new Intent().setClass(this, Offers.class);
		spec = tabHost.newTabSpec("offers")
				.setIndicator("Ponudbe", res.getDrawable(R.drawable.pir))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, Pubs.class);
		spec = tabHost.newTabSpec("pubs")
				.setIndicator("Lokali", res.getDrawable(R.drawable.list))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);
	}
}